home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / DOS / TELECOMM / PCCP047 / RFXTOPCX.C < prev    next >
C/C++ Source or Header  |  1992-08-28  |  37KB  |  1,450 lines

  1. #include<stdio.h>
  2. #include<fcntl.h>
  3. #include<sys\types.h>
  4. #include<sys\stat.h>
  5. #include<setjmp.h>
  6.  
  7. #define MAKEUP 0x1000
  8. #define SEVENOHS 0x1fff
  9.  
  10. unsigned char curbyte, bitmask;
  11.  
  12. int rfxfd;
  13.  
  14. jmp_buf where;
  15.  
  16. int debug;
  17.  
  18. int getbit()
  19.         {
  20.         int result;
  21.         if(!bitmask)
  22.                 {
  23.                 if(!read(rfxfd, &curbyte, 1))
  24.             {
  25.             printf("Premature end of file.\n");
  26.             exit(1);
  27.             }
  28.         if(curbyte==0x10)
  29.             {
  30.             if(!read(rfxfd, &curbyte, 1))
  31.                 {
  32.                 printf("Premature end of file.\n");
  33.                 exit(1);
  34.                 }
  35.             if(curbyte==0x03)
  36.                 {
  37.                 longjmp(where, -1);
  38.                 }
  39.             }
  40.                 bitmask=0x80;
  41.         if(debug)
  42.             printf("(%02x)", curbyte);
  43.                 }
  44.         result=curbyte&bitmask;
  45.         bitmask>>=1;
  46.     bitmask&=0x7f;
  47.     if(debug)
  48.         if(result)
  49.             putchar('1');
  50.         else
  51.             putchar('0');
  52.     return(result);
  53.         }
  54.  
  55. int getwhite()
  56.         {
  57.         if(getbit())
  58.          /* 1 */
  59.          if(getbit())
  60.           /* 11 */
  61.           if(getbit())
  62.            /* 111 */
  63.            if(getbit())
  64.             /* 1111 */
  65.             return(7);
  66.            else
  67.             /* 1110 */
  68.             return(6);
  69.           else
  70.            /* 110 */
  71.            if(getbit())
  72.             /* 1101 */
  73.             if(getbit())
  74.              /* 11011 */
  75.              return(MAKEUP|64);
  76.             else
  77.              /* 11010 */
  78.              if(getbit())
  79.               /* 110101 */
  80.               return(15);
  81.              else
  82.               /* 110100 */
  83.               return(14);
  84.            else
  85.             /* 1100 */
  86.             return(5);
  87.          else
  88.           /* 10 */
  89.           if(getbit())
  90.            /* 101 */
  91.            if(getbit())
  92.             /* 1011 */
  93.             return(4);
  94.            else
  95.             /* 1010 */
  96.             if(getbit())
  97.              /* 10101 */
  98.              if(getbit())
  99.               /* 101011 */
  100.               return(17);
  101.              else
  102.               /* 101010 */
  103.               return(16);
  104.             else
  105.              /* 10100 */
  106.              return(9);
  107.           else
  108.            /* 100 */
  109.            if(getbit())
  110.             /* 1001 */
  111.             if(getbit())
  112.              /* 10011 */
  113.              return(8);
  114.             else
  115.              /* 10010 */
  116.              return(MAKEUP|128);
  117.            else
  118.             /* 1000 */
  119.             return(3);
  120.         else
  121.          /* 0 */
  122.          if(getbit())
  123.           /* 01 */
  124.           if(getbit())
  125.            /* 011 */
  126.            if(getbit())
  127.             /* 0111 */
  128.             return(2);
  129.            else
  130.             /* 0110 */
  131.             if(getbit())
  132.              /* 01101 */
  133.              if(getbit())
  134.               /* 011011 */
  135.               if(getbit())
  136.                /* 0110111 */
  137.                return(MAKEUP|256);
  138.               else
  139.                /* 0110110 */
  140.                if(getbit())
  141.                 /* 01101101 */
  142.                 if(getbit())
  143.                  /* 011011011 */
  144.                  return(MAKEUP|1408);
  145.                 else
  146.                  /* 011011010 */
  147.                  return(MAKEUP|1344);
  148.                else
  149.                 /* 01101100 */
  150.                 if(getbit())
  151.                  /* 011011001 */
  152.                  return(MAKEUP|1280);
  153.                 else
  154.                  /* 011011000 */
  155.                  return(MAKEUP|1216);
  156.              else
  157.               /* 011010 */
  158.               if(getbit())
  159.                /* 0110101 */
  160.                if(getbit())
  161.                 /* 01101011 */
  162.                 if(getbit())
  163.                  /* 011010111 */
  164.                  return(MAKEUP|1152);
  165.                 else
  166.                  /* 011010110 */
  167.                  return(MAKEUP|1088);
  168.                else
  169.                 /* 01101010 */
  170.                 if(getbit())
  171.                  /* 011010101 */
  172.                  return(MAKEUP|1024);
  173.                 else
  174.                  /* 011010100 */
  175.                  return(MAKEUP|960);
  176.               else
  177.                /* 0110100 */
  178.                if(getbit())
  179.                 /* 01101001 */
  180.                 if(getbit())
  181.                  /* 011010011 */
  182.                  return(MAKEUP|896);
  183.                 else
  184.                  /* 011010010 */
  185.                  return(MAKEUP|832);
  186.                else
  187.                 /* 01101000 */
  188.                 return(MAKEUP|576);
  189.             else
  190.              /* 01100 */
  191.              if(getbit())
  192.               /* 011001 */
  193.               if(getbit())
  194.                /* 0110011 */
  195.                if(getbit())
  196.                 /* 01100111 */
  197.                 return(MAKEUP|640);
  198.                else
  199.                 /* 01100110 */
  200.                 if(getbit())
  201.                  /* 011001101 */
  202.                  return(MAKEUP|768);
  203.                 else
  204.                  /* 011001100 */
  205.                  return(MAKEUP|704);
  206.               else
  207.                /* 0110010 */
  208.                if(getbit())
  209.                 /* 01100101 */
  210.                 return(MAKEUP|512);
  211.                else
  212.                 /* 01100100 */
  213.                 return(MAKEUP|448);
  214.              else
  215.               /* 011000 */
  216.               return(MAKEUP|1664);
  217.           else
  218.            /* 010 */
  219.            if(getbit())
  220.             /* 0101 */
  221.             if(getbit())
  222.              /* 01011 */
  223.              if(getbit())
  224.               /* 010111 */
  225.               return(MAKEUP|192);
  226.              else
  227.               /* 010110 */
  228.               if(getbit())
  229.                /* 0101101 */
  230.                if(getbit())
  231.                 /* 01011011 */
  232.                 return(58);
  233.                else
  234.                 /* 01011010 */
  235.                 return(57);
  236.               else
  237.                /* 0101100 */
  238.                if(getbit())
  239.                 /* 01011001 */
  240.                 return(56);
  241.                else
  242.                 /* 01011000 */
  243.                 return(55);
  244.             else
  245.              /* 01010 */
  246.              if(getbit())
  247.               /* 010101 */
  248.               if(getbit())
  249.                /* 0101011 */
  250.                return(25);
  251.               else
  252.                /* 0101010 */
  253.                if(getbit())
  254.                 /* 01010101 */
  255.                 return(52);
  256.                else
  257.                 /* 01010100 */
  258.                 return(51);
  259.              else
  260.               /* 010100 */
  261.               if(getbit())
  262.                /* 0101001 */
  263.                if(getbit())
  264.                 /* 01010011 */
  265.                 return(50);
  266.                else
  267.                 /* 01010010 */
  268.                 return(49);
  269.               else
  270.                /* 0101000 */
  271.                return(24);
  272.            else
  273.             /* 0100 */
  274.             if(getbit())
  275.              /* 01001 */
  276.              if(getbit())
  277.               /* 010011 */
  278.               if(getbit())
  279.                /* 0100111 */
  280.                return(18);
  281.               else
  282.                /* 0100110 */
  283.                if(getbit())
  284.                 /* 01001101 */
  285.                 if(getbit())
  286.                  /* 010011011 */
  287.                  return(MAKEUP|1728);
  288.                 else
  289.                  /* 010011010 */
  290.                  return(MAKEUP|1600);
  291.                else
  292.                 /* 01001100 */
  293.                 if(getbit())
  294.                  /* 010011001 */
  295.                  return(MAKEUP|1536);
  296.                 else
  297.                  /* 010011000 */
  298.                  return(MAKEUP|1472);
  299.              else
  300.               /* 010010 */
  301.               if(getbit())
  302.                /* 0100101 */
  303.                if(getbit())
  304.                 /* 01001011 */
  305.                 return(60);
  306.                else
  307.                 /* 01001010 */
  308.                 return(59);
  309.               else
  310.                /* 0100100 */
  311.                return(27);
  312.             else
  313.              /* 01000 */
  314.              return(11);
  315.          else
  316.           /* 00 */
  317.           if(getbit())
  318.            /* 001 */
  319.            if(getbit())
  320.             /* 0011 */
  321.             if(getbit())
  322.              /* 00111 */
  323.              return(10);
  324.             else
  325.              /* 00110 */
  326.              if(getbit())
  327.               /* 001101 */
  328.               if(getbit())
  329.                /* 0011011 */
  330.                if(getbit())
  331.                 /* 00110111 */
  332.                 return(MAKEUP|384);
  333.                else
  334.                 /* 00110110 */
  335.                 return(MAKEUP|320);
  336.               else
  337.                /* 0011010 */
  338.                if(getbit())
  339.                 /* 00110101 */
  340.                 return(0);
  341.                else
  342.                 /* 00110100 */
  343.                 return(63);
  344.              else
  345.               /* 001100 */
  346.               if(getbit())
  347.                /* 0011001 */
  348.                if(getbit())
  349.                 /* 00110011 */
  350.                 return(62);
  351.                else
  352.                 /* 00110010 */
  353.                 return(61);
  354.               else
  355.                /* 0011000 */
  356.                return(28);
  357.            else
  358.             /* 0010 */
  359.             if(getbit())
  360.              /* 00101 */
  361.              if(getbit())
  362.               /* 001011 */
  363.               if(getbit())
  364.                /* 0010111 */
  365.                return(21);
  366.               else
  367.                /* 0010110 */
  368.                if(getbit())
  369.                 /* 00101101 */
  370.                 return(44);
  371.                else
  372.                 /* 00101100 */
  373.                 return(43);
  374.              else
  375.               /* 001010 */
  376.               if(getbit())
  377.                /* 0010101 */
  378.                if(getbit())
  379.                 /* 00101011 */
  380.                 return(42);
  381.                else
  382.                 /* 00101010 */
  383.                 return(41);
  384.               else
  385.                /* 0010100 */
  386.                if(getbit())
  387.                 /* 00101001 */
  388.                 return(40);
  389.                else
  390.                 /* 00101000 */
  391.                 return(39);
  392.             else
  393.              /* 00100 */
  394.              if(getbit())
  395.               /* 001001 */
  396.               if(getbit())
  397.                /* 0010011 */
  398.                return(26);
  399.               else
  400.                /* 0010010 */
  401.                if(getbit())
  402.                 /* 00100101 */
  403.                 return(54);
  404.                else
  405.                 /* 00100100 */
  406.                 return(53);
  407.              else
  408.               /* 001000 */
  409.               return(12);
  410.           else
  411.            /* 000 */
  412.            if(getbit())
  413.             /* 0001 */
  414.             if(getbit())
  415.              /* 00011 */
  416.              if(getbit())
  417.               /* 000111 */
  418.               return(1);
  419.              else
  420.               /* 000110 */
  421.               if(getbit())
  422.                /* 0001101 */
  423.                if(getbit())
  424.                 /* 00011011 */
  425.                 return(32);
  426.                else
  427.                 /* 00011010 */
  428.                 return(31);
  429.               else
  430.                /* 0001100 */
  431.                return(19);
  432.             else
  433.              /* 00010 */
  434.              if(getbit())
  435.               /* 000101 */
  436.               if(getbit())
  437.                /* 0001011 */
  438.                if(getbit())
  439.                 /* 00010111 */
  440.                 return(38);
  441.                else
  442.                 /* 00010110 */
  443.                 return(37);
  444.               else
  445.                /* 0001010 */
  446.                if(getbit())
  447.                 /* 00010101 */
  448.                 return(36);
  449.                else
  450.                 /* 00010100 */
  451.                 return(35);
  452.              else
  453.               /* 000100 */
  454.               if(getbit())
  455.                /* 0001001 */
  456.                if(getbit())
  457.                 /* 00010011 */
  458.                 return(34);
  459.                else
  460.                 /* 00010010 */
  461.                 return(33);
  462.               else
  463.                /* 0001000 */
  464.                return(20);
  465.            else
  466.             /* 0000 */
  467.             if(getbit())
  468.              /* 00001 */
  469.              if(getbit())
  470.               /* 000011 */
  471.               return(13);
  472.              else
  473.               /* 000010 */
  474.               if(getbit())
  475.                /* 0000101 */
  476.                if(getbit())
  477.                 /* 00001011 */
  478.                 return(48);
  479.                else
  480.                 /* 00001010 */
  481.                 return(47);
  482.               else
  483.                /* 0000100 */
  484.                return(23);
  485.             else
  486.              /* 00000 */
  487.              if(getbit())
  488.               /* 000001 */
  489.               if(getbit())
  490.                /* 0000011 */
  491.                return(22);
  492.               else
  493.                /* 0000010 */
  494.                if(getbit())
  495.                 /* 00000101 */
  496.                 return(46);
  497.                else
  498.                 /* 00000100 */
  499.                 return(45);
  500.              else
  501.               /* 000000 */
  502.               if(getbit())
  503.                /* 0000001 */
  504.                if(getbit())
  505.                 /* 00000011 */
  506.                 return(30);
  507.                else
  508.                 /* 00000010 */
  509.                 return(29);
  510.               else
  511.                /* 0000000 */
  512.                return(SEVENOHS);
  513.         }
  514.  
  515. int getblack()
  516.     {
  517.     if(getbit())
  518.          /* 1 */
  519.          if(getbit())
  520.           /* 11 */
  521.           return(2);
  522.          else
  523.           /* 10 */
  524.           return(3);
  525.         else
  526.          /* 0 */
  527.          if(getbit())
  528.           /* 01 */
  529.           if(getbit())
  530.            /* 011 */
  531.            return(4);
  532.           else
  533.            /* 010 */
  534.            return(1);
  535.          else
  536.           /* 00 */
  537.           if(getbit())
  538.            /* 001 */
  539.            if(getbit())
  540.             /* 0011 */
  541.             return(5);
  542.            else
  543.             /* 0010 */
  544.             return(6);
  545.           else
  546.            /* 000 */
  547.            if(getbit())
  548.             /* 0001 */
  549.             if(getbit())
  550.              /* 00011 */
  551.              return(7);
  552.             else
  553.              /* 00010 */
  554.              if(getbit())
  555.               /* 000101 */
  556.               return(8);
  557.              else
  558.               /* 000100 */
  559.               return(9);
  560.            else
  561.             /* 0000 */
  562.             if(getbit())
  563.              /* 00001 */
  564.              if(getbit())
  565.               /* 000011 */
  566.               if(getbit())
  567.                /* 0000111 */
  568.                return(12);
  569.               else
  570.                /* 0000110 */
  571.                if(getbit())
  572.                 /* 00001101 */
  573.                 if(getbit())
  574.                  /* 000011011 */
  575.                  if(getbit())
  576.                   /* 0000110111 */
  577.                   return(0);
  578.                  else
  579.                   /* 0000110110 */
  580.                   if(getbit())
  581.                    /* 00001101101 */
  582.                    if(getbit())
  583.                     /* 000011011011 */
  584.                     return(43);
  585.                    else
  586.                     /* 000011011010 */
  587.                     return(42);
  588.                   else
  589.                    /* 00001101100 */
  590.                    return(21);
  591.                 else
  592.                  /* 000011010 */
  593.                  if(getbit())
  594.                   /* 0000110101 */
  595.                   if(getbit())
  596.                    /* 00001101011 */
  597.                    if(getbit())
  598.                     /* 000011010111 */
  599.                     return(39);
  600.                    else
  601.                     /* 000011010110 */
  602.                     return(38);
  603.                   else
  604.                    /* 00001101010 */
  605.                    if(getbit())
  606.                     /* 000011010101 */
  607.                     return(37);
  608.                    else
  609.                     /* 000011010100 */
  610.                     return(36);
  611.                  else
  612.                   /* 0000110100 */
  613.                   if(getbit())
  614.                    /* 00001101001 */
  615.                    if(getbit())
  616.                     /* 000011010011 */
  617.                     return(35);
  618.                    else
  619.                     /* 000011010010 */
  620.                     return(34);
  621.                   else
  622.                    /* 00001101000 */
  623.                    return(20);
  624.                else
  625.                 /* 00001100 */
  626.                 if(getbit())
  627.                  /* 000011001 */
  628.                  if(getbit())
  629.                   /* 0000110011 */
  630.                   if(getbit())
  631.                    /* 00001100111 */
  632.                    return(19);
  633.                   else
  634.                    /* 00001100110 */
  635.                    if(getbit())
  636.                     /* 000011001101 */
  637.                     return(29);
  638.                    else
  639.                     /* 000011001100 */
  640.                     return(28);
  641.                  else
  642.                   /* 0000110010 */
  643.                   if(getbit())
  644.                    /* 00001100101 */
  645.                    if(getbit())
  646.                     /* 000011001011 */
  647.                     return(27);
  648.                    else
  649.                     /* 000011001010 */
  650.                     return(26);
  651.                   else
  652.                    /* 00001100100 */
  653.                    if(getbit())
  654.                     /* 000011001001 */
  655.                     return(MAKEUP|192);
  656.                    else
  657.                     /* 000011001000 */
  658.                     return(MAKEUP|128);
  659.                 else
  660.                  /* 000011000 */
  661.                  return(15);
  662.              else
  663.               /* 000010 */
  664.               if(getbit())
  665.                /* 0000101 */
  666.                return(11);
  667.               else
  668.                /* 0000100 */
  669.                return(10);
  670.             else
  671.              /* 00000 */
  672.              if(getbit())
  673.               /* 000001 */
  674.               if(getbit())
  675.                /* 0000011 */
  676.                if(getbit())
  677.                 /* 00000111 */
  678.                 return(14);
  679.                else
  680.                 /* 00000110 */
  681.                 if(getbit())
  682.                  /* 000001101 */
  683.                  if(getbit())
  684.                   /* 0000011011 */
  685.                   if(getbit())
  686.                    /* 00000110111 */
  687.                    return(22);
  688.                   else
  689.                    /* 00000110110 */
  690.                    if(getbit())
  691.                     /* 000001101101 */
  692.                     return(41);
  693.                    else
  694.                     /* 000001101100 */
  695.                     return(40);
  696.                  else
  697.                   /* 0000011010 */
  698.                   if(getbit())
  699.                    /* 00000110101 */
  700.                    if(getbit())
  701.                     /* 000001101011 */
  702.                     return(33);
  703.                    else
  704.                     /* 000001101010 */
  705.                     return(32);
  706.                   else
  707.                    /* 00000110100 */
  708.                    if(getbit())
  709.                     /* 000001101001 */
  710.                     return(31);
  711.                    else
  712.                     /* 000001101000 */
  713.                     return(30);
  714.                 else
  715.                  /* 000001100 */
  716.                  if(getbit())
  717.                   /* 0000011001 */
  718.                   if(getbit())
  719.                    /* 00000110011 */
  720.                    if(getbit())
  721.                     /* 000001100111 */
  722.                     return(63);
  723.                    else
  724.                     /* 000001100110 */
  725.                     return(62);
  726.                   else
  727.                    /* 00000110010 */
  728.                    if(getbit())
  729.                     /* 000001100101 */
  730.                     return(49);
  731.                    else
  732.                     /* 000001100100 */
  733.                     return(48);
  734.                  else
  735.                   /* 0000011000 */
  736.                   return(17);
  737.               else
  738.                /* 0000010 */
  739.                if(getbit())
  740.                 /* 00000101 */
  741.                 if(getbit())
  742.                  /* 000001011 */
  743.                  if(getbit())
  744.                   /* 0000010111 */
  745.                   return(16);
  746.                  else
  747.                   /* 0000010110 */
  748.                   if(getbit())
  749.                    /* 00000101101 */
  750.                    if(getbit())
  751.                     /* 000001011011 */
  752.                     return(MAKEUP|256);
  753.                    else
  754.                     /* 000001011010 */
  755.                     return(61);
  756.                   else
  757.                    /* 00000101100 */
  758.                    if(getbit())
  759.                     /* 000001011001 */
  760.                     return(58);
  761.                    else
  762.                     /* 000001011000 */
  763.                     return(57);
  764.                 else
  765.                  /* 000001010 */
  766.                  if(getbit())
  767.                   /* 0000010101 */
  768.                   if(getbit())
  769.                    /* 00000101011 */
  770.                    if(getbit())
  771.                     /* 000001010111 */
  772.                     return(47);
  773.                    else
  774.                     /* 000001010110 */
  775.                     return(46);
  776.                   else
  777.                    /* 00000101010 */
  778.                    if(getbit())
  779.                     /* 000001010101 */
  780.                     return(45);
  781.                    else
  782.                     /* 000001010100 */
  783.                     return(44);
  784.                  else
  785.                   /* 0000010100 */
  786.                   if(getbit())
  787.                    /* 00000101001 */
  788.                    if(getbit())
  789.                     /* 000001010011 */
  790.                     return(51);
  791.                    else
  792.                     /* 000001010010 */
  793.                     return(50);
  794.                   else
  795.                    /* 00000101000 */
  796.                    return(23);
  797.                else
  798.                 /* 00000100 */
  799.                 return(13);
  800.              else
  801.               /* 000000 */
  802.               if(getbit())
  803.                /* 0000001 */
  804.                if(getbit())
  805.                 /* 00000011 */
  806.                 if(getbit())
  807.                  /* 000000111 */
  808.                  if(getbit())
  809.                   /* 0000001111 */
  810.                   return(MAKEUP|64);
  811.                  else
  812.                   /* 0000001110 */
  813.                   if(getbit())
  814.                    /* 00000011101 */
  815.                    if(getbit())
  816.                     /* 000000111011 */
  817.                     if(getbit())
  818.                      /* 0000001110111 */
  819.                      return(MAKEUP|1216);
  820.                     else
  821.                      /* 0000001110110 */
  822.                      return(MAKEUP|1152);
  823.                    else
  824.                     /* 000000111010 */
  825.                     if(getbit())
  826.                      /* 0000001110101 */
  827.                      return(MAKEUP|1088);
  828.                     else
  829.                      /* 0000001110100 */
  830.                      return(MAKEUP|1024);
  831.                   else
  832.                    /* 00000011100 */
  833.                    if(getbit())
  834.                     /* 000000111001 */
  835.                     if(getbit())
  836.                      /* 0000001110011 */
  837.                      return(MAKEUP|960);
  838.                     else
  839.                      /* 0000001110010 */
  840.                      return(MAKEUP|896);
  841.                    else
  842.                     /* 000000111000 */
  843.                     return(54);
  844.                 else
  845.                  /* 000000110 */
  846.                  if(getbit())
  847.                   /* 0000001101 */
  848.                   if(getbit())
  849.                    /* 00000011011 */
  850.                    if(getbit())
  851.                     /* 000000110111 */
  852.                     return(53);
  853.                    else
  854.                     /* 000000110110 */
  855.                     if(getbit())
  856.                      /* 0000001101101 */
  857.                      return(MAKEUP|576);
  858.                     else
  859.                      /* 0000001101100 */
  860.                      return(MAKEUP|512);
  861.                   else
  862.                    /* 00000011010 */
  863.                    if(getbit())
  864.                     /* 000000110101 */
  865.                     return(MAKEUP|448);
  866.                    else
  867.                     /* 000000110100 */
  868.                     return(MAKEUP|384);
  869.                  else
  870.                   /* 0000001100 */
  871.                   if(getbit())
  872.                    /* 00000011001 */
  873.                    if(getbit())
  874.                     /* 000000110011 */
  875.                     return(MAKEUP|320);
  876.                    else
  877.                     /* 000000110010 */
  878.                     if(getbit())
  879.                      /* 0000001100101 */
  880.                      return(MAKEUP|1728);
  881.                     else
  882.                      /* 0000001100100 */
  883.                      return(MAKEUP|1664);
  884.                   else
  885.                    /* 00000011000 */
  886.                    return(25);
  887.                else
  888.                 /* 00000010 */
  889.                 if(getbit())
  890.                  /* 000000101 */
  891.                  if(getbit())
  892.                   /* 0000001011 */
  893.                   if(getbit())
  894.                    /* 00000010111 */
  895.                    return(24);
  896.                   else
  897.                    /* 00000010110 */
  898.                    if(getbit())
  899.                     /* 000000101101 */
  900.                     if(getbit())
  901.                      /* 0000001011011 */
  902.                      return(MAKEUP|1600);
  903.                     else
  904.                      /* 0000001011010 */
  905.                      return(MAKEUP|1536);
  906.                    else
  907.                     /* 000000101100 */
  908.                     return(60);
  909.                  else
  910.                   /* 0000001010 */
  911.                   if(getbit())
  912.                    /* 00000010101 */
  913.                    if(getbit())
  914.                     /* 000000101011 */
  915.                     return(59);
  916.                    else
  917.                     /* 000000101010 */
  918.                     if(getbit())
  919.                      /* 0000001010101 */
  920.                      return(MAKEUP|1472);
  921.                     else
  922.                      /* 0000001010100 */
  923.                      return(MAKEUP|1408);
  924.                   else
  925.                    /* 00000010100 */
  926.                    if(getbit())
  927.                     /* 000000101001 */
  928.                     if(getbit())
  929.                      /* 0000001010011 */
  930.                      return(MAKEUP|1344);
  931.                     else
  932.                      /* 0000001010010 */
  933.                      return(MAKEUP|1280);
  934.                    else
  935.                     /* 000000101000 */
  936.                     return(56);
  937.                 else
  938.                  /* 000000100 */
  939.                  if(getbit())
  940.                   /* 0000001001 */
  941.                   if(getbit())
  942.                    /* 00000010011 */
  943.                    if(getbit())
  944.                     /* 000000100111 */
  945.                     return(55);
  946.                    else
  947.                     /* 000000100110 */
  948.                     if(getbit())
  949.                      /* 0000001001101 */
  950.                      return(MAKEUP|832);
  951.                     else
  952.                      /* 0000001001100 */
  953.                      return(MAKEUP|768);
  954.                   else
  955.                    /* 00000010010 */
  956.                    if(getbit())
  957.                     /* 000000100101 */
  958.                     if(getbit())
  959.                      /* 0000001001011 */
  960.                      return(MAKEUP|704);
  961.                     else
  962.                      /* 0000001001010 */
  963.                      return(MAKEUP|640);
  964.                    else
  965.                     /* 000000100100 */
  966.                     return(52);
  967.                  else
  968.                   /* 0000001000 */
  969.                   return(18);
  970.               else
  971.                /* 0000000 */
  972.                return(SEVENOHS);
  973.        }
  974.  
  975. struct header_s
  976.     {
  977.     unsigned char mfgr;
  978.     unsigned char version;
  979.     unsigned char encoding;
  980.     unsigned char bppix;
  981.     unsigned short xmin;
  982.     unsigned short ymin;
  983.     unsigned short xmax;
  984.     unsigned short ymax;
  985.     unsigned short hdpi;
  986.     unsigned short vdpi;
  987.     unsigned char cmap[48];
  988.     unsigned char reserved;
  989.     unsigned char nplanes;
  990.     unsigned short bypl;
  991.     unsigned short paltinf;
  992.     unsigned short hscrnsz;
  993.     unsigned short vscrnsz;
  994.     unsigned char filler[54];
  995.     };
  996.  
  997. int pcxfd, pagelines;
  998. struct header_s header;
  999.  
  1000. pcxwriteheader()
  1001.     {
  1002.     int i;
  1003.     header.mfgr=10;
  1004.     header.version=45;
  1005.     header.encoding=1;
  1006.     header.bppix=1;
  1007.     header.xmin=0;
  1008.     header.ymin=0;
  1009.     header.xmax=1727;
  1010.     header.ymax=pagelines-1;
  1011.     header.hdpi=200;
  1012.     header.vdpi=192;
  1013.     header.reserved=0;
  1014.     header.nplanes=1;
  1015.     header.bypl=216;
  1016.     header.paltinf=1;
  1017.     header.hscrnsz=0;
  1018.     header.vscrnsz=0;
  1019.     for(i=0;i<54;++i)
  1020.         header.filler[i]=0;
  1021.     lseek(pcxfd, (long)0, SEEK_SET);
  1022.     if(write(pcxfd, &header, 128)!=128)
  1023.         {
  1024.         printf("Error overwriting final pcx file header.\n");
  1025.         exit(9);
  1026.         }
  1027.     }
  1028.  
  1029. unsigned char fcfr[]="+FCFR\r\n\r\n";
  1030. unsigned char fhng[]="+FHNG";
  1031. unsigned char connect[]="CONNECT\r\n";
  1032.  
  1033. int scanfcfr()
  1034.     {
  1035.     int i, j, k;
  1036.     unsigned char c;
  1037.     i=j=k=0;
  1038.     while(read(rfxfd, &c, 1))
  1039.         {
  1040.         if(fcfr[i]==c)
  1041.             {
  1042.             i++;
  1043.             if(fcfr[i]=='\0')
  1044.                 return(1);
  1045.             }
  1046.         else
  1047.             i=0;
  1048.         if(connect[k]==c)
  1049.             {
  1050.             k++;
  1051.             if(connect[k]=='\0')
  1052.                 return(0);
  1053.             }
  1054.         else
  1055.             k=0;
  1056.         if(fhng[j]==c)
  1057.             {
  1058.             j++;
  1059.             if(fhng[j]=='\0')
  1060.                 {
  1061.                 printf("End of document. Exit code +FHNG");
  1062.                 while(1)
  1063.                     {
  1064.                     if(!read(rfxfd, &c, 1))
  1065.                         {
  1066.                         printf("\nPremature end of file while seeking +FHNG code.\n");
  1067.                         exit(6);
  1068.                         }
  1069.                     putchar(c);
  1070.                     if(c=='\n')
  1071.                         break;
  1072.                     }
  1073.                 exit(0);
  1074.                 }
  1075.             }
  1076.         else
  1077.             j=0;
  1078.         }
  1079.     printf("Premature end of file while seeking +FCFR.\n");
  1080.     exit(5);
  1081.     }
  1082.  
  1083. int scanconnect()
  1084.     {
  1085.     int i;
  1086.     unsigned char c;
  1087.     i=0;
  1088.     while(read(rfxfd, &c, 1))
  1089.         {
  1090.         if(connect[i]==c)
  1091.             {
  1092.             i++;
  1093.             if(connect[i]=='\0')
  1094.                 return(0);
  1095.             }
  1096.         else
  1097.             i=0;
  1098.         }
  1099.     printf("Premature end of file while seeking CONNECT.\n");
  1100.     exit(7);
  1101.     }
  1102.  
  1103. int rfxgets(str)
  1104.     unsigned char *str;
  1105.     {
  1106.     int i;
  1107.     i=0;
  1108.     while(read(rfxfd, &str[i], 1))
  1109.         if(str[i]=='\r')
  1110.             {
  1111.             str[i]='\0';
  1112.             return(i);
  1113.             }
  1114.         else
  1115.             i++;
  1116.     return(-1);
  1117.     }
  1118.  
  1119. unsigned char line[216];
  1120. int linenbits;
  1121.  
  1122. putwhite(n)
  1123.     int n;
  1124.     {
  1125.     int bytenum, i;
  1126.     if(n>(1728-linenbits))
  1127.         n=1728-linenbits;
  1128.     bytenum=linenbits/8; /* integer division */
  1129.     for(i=7-(linenbits%8);(i>=0)&&(n>0);i--,n--)
  1130.         {
  1131.         line[bytenum]|=1<<i;
  1132.         linenbits++;
  1133.         }
  1134.     bytenum=linenbits/8;
  1135.     i=n/8;
  1136.     if(n%8)
  1137.         i++;
  1138.     for(;i>0;--i)
  1139.         line[bytenum++]=0xff; /* We limited n above */
  1140.     linenbits+=n;
  1141.     }
  1142.  
  1143. putblack(n)
  1144.     int n;
  1145.     {
  1146.     int bytenum, i;
  1147.     if(n>(1728-linenbits))
  1148.         n=1728-linenbits;
  1149.     bytenum=linenbits/8; /* integer division */
  1150.     for(i=7-(linenbits%8);(i>=0)&&(n>0);i--,n--)
  1151.         {
  1152.         line[bytenum]&=~(1<<i);
  1153.         linenbits++;
  1154.         }
  1155.     bytenum=linenbits/8;
  1156.     i=n/8;
  1157.     if(n%8)
  1158.         i++;
  1159.     for(;i>0;--i)
  1160.         line[bytenum++]=0x00;
  1161.     linenbits+=n;
  1162.     }
  1163.  
  1164. pcxwriteline(times)
  1165.     int times;
  1166.     {
  1167.     unsigned char pcxline[432], hold;
  1168.     int pcxnbytes, i, nreps;
  1169.     if(!linenbits)
  1170.         return(0);
  1171.     if(linenbits<1728)
  1172.         putwhite(1728-linenbits);
  1173.     pcxnbytes=0;
  1174.     /* PCX Encode */
  1175.     hold=line[0];
  1176.     nreps=i=1;
  1177.     while(1)
  1178.         {
  1179.         if((i<216)&&(line[i]==hold)) /* Reading beyond item is OK*/
  1180.             nreps++;
  1181.         else
  1182.             {
  1183.             if((nreps==1)&&((hold&0xc0)!=0xc0))
  1184.                 pcxline[pcxnbytes++]=hold;
  1185.             else
  1186.                 {
  1187.                 while(nreps)
  1188.                     {
  1189.                     if(nreps>63)
  1190.                         {
  1191.                         pcxline[pcxnbytes++]=63|0xc0;
  1192.                         pcxline[pcxnbytes++]=hold;
  1193.                         nreps-=63;
  1194.                         }
  1195.                     else
  1196.                         {
  1197.                         pcxline[pcxnbytes++]=nreps|0xc0;
  1198.                         pcxline[pcxnbytes++]=hold;
  1199.                         nreps=0;
  1200.                         }
  1201.                     }
  1202.                 nreps=1; /* This is a little ahead */
  1203.                 }
  1204.             }
  1205.         if(i<216)
  1206.             hold=line[i++];
  1207.         else
  1208.             break;
  1209.         }
  1210.     /* Write encoded line */
  1211.     for(i=0;i<times;i++)
  1212.         {
  1213.         if(write(pcxfd, pcxline, pcxnbytes)!=pcxnbytes)
  1214.             {
  1215.             printf("Error writing encoded line to file.\n");
  1216.             exit(8);
  1217.             }
  1218.         pagelines++;
  1219.         }
  1220.     }
  1221.  
  1222. abortpage()
  1223.     {
  1224.     unsigned char c;
  1225.     printf("Flushing page...\n");
  1226.     while(1)
  1227.         {
  1228.         if(!read(rfxfd, &c, 1))
  1229.             {
  1230.             printf("Premature end of file while flushing bad page.\n");
  1231.             exit(30);
  1232.             }
  1233.         if(c==0x10)
  1234.             {
  1235.             if(!read(rfxfd, &c, 1))
  1236.                 {
  1237.                 printf("Premature end of file while flushing bad page.\n");
  1238.                 exit(30);
  1239.                 }
  1240.             if(c==0x03)
  1241.                 break;
  1242.             }
  1243.         }
  1244.     }
  1245.  
  1246.  
  1247. main(argc, argv)
  1248.     int argc;
  1249.     char **argv;
  1250.     {
  1251.     int dcsvr, dcswd, dcsdf, page, symbol, bitcount;
  1252.     int dopage, npelschk, gotfcfr;
  1253.     unsigned char c, str[128];
  1254.     printf("Copyright (C) 1992 Peter Edward Cann\n\n");
  1255.     if((argc!=2)&&(argc!=3))
  1256.         {
  1257.         printf("USAGE: rfx2pcx <rfx filename> [/debug]\n");
  1258.         exit(10);
  1259.         }
  1260.     if(argc==3)
  1261.         debug=1;
  1262.     else
  1263.         debug=0;
  1264.     if((rfxfd=open(argv[1], O_RDONLY|O_BINARY))==-1)
  1265.         {
  1266.         printf("Error opening %s for read.\n", argv[1]);
  1267.         exit(11);
  1268.         }
  1269.     while(1)
  1270.         {
  1271.         if(!read(rfxfd, &c, 1))
  1272.             {
  1273.             printf("Premature end of file while seeking initial doublequote.\n");
  1274.             exit(12);
  1275.             }
  1276.         if(c=='"')
  1277.             break;
  1278.         }
  1279.     printf("Sender ID: ");
  1280.     while(1)
  1281.         {
  1282.         if(!read(rfxfd, &c, 1))
  1283.             {
  1284.             printf("Premature end of file while seeking final doublequote.\n");
  1285.             exit(13);
  1286.             }
  1287.         if(c=='"')
  1288.             break;
  1289.         putchar(c);
  1290.         }
  1291.     putchar('\n');
  1292.     page=0;
  1293.     gotfcfr=0;
  1294.     while(1)
  1295.         {
  1296.         page++;
  1297.         if(scanfcfr()) /* Normal exit is in this function */
  1298.             {
  1299.             if(rfxgets(str)==-1)
  1300.                 {
  1301.                 printf("Premature end of file while seeking +FDCS.\n");
  1302.                 exit(15);
  1303.                 }
  1304.             if(sscanf(str, " +FDCS: %d,%*d,%d,%*d,%d,%*d,%*d,%*d",
  1305.                 &dcsvr, &dcswd, &dcsdf)!=3)
  1306.                 {
  1307.                 printf("Error scanning +FDCS params.\n");
  1308.                 exit(14);
  1309.                 }
  1310.             else
  1311.                 gotfcfr=1;
  1312.             if(dcsvr)
  1313.                 printf("High vertical resolution.\n");
  1314.             else
  1315.                 printf("Standard vertical resolution.\n");
  1316.             if(dcswd)
  1317.                 {
  1318.                 printf("Non-supported width!\n");
  1319.                 exit(13);
  1320.                 }
  1321.             if(dcsdf)
  1322.                 {
  1323.                 printf("Non-supported coding scheme (must be 1-D)!\n");
  1324.                 exit(14);
  1325.                 }
  1326.             scanconnect();
  1327.             gotfcfr=1;
  1328.             }
  1329.         if(!gotfcfr)
  1330.             {
  1331.             printf("Never got initial +FCFR; aborting.\n");
  1332.             exit(60);
  1333.             }
  1334.         bitmask=0;
  1335.         while(1)
  1336.             {
  1337.             printf("Enter filename for Page %d (include .PCX):\n--> ", page);
  1338.             gets(str);
  1339.             if((pcxfd=open(str, O_CREAT|O_BINARY|O_TRUNC|O_WRONLY, S_IWRITE))!=-1)
  1340.                 break;
  1341.             else
  1342.                 printf("Error opening file.\n");
  1343.             }
  1344.         /* Save a dummy block for PCX file header */
  1345.         if(write(pcxfd, str, 128)!=128)
  1346.             {
  1347.             printf("Error writing dummy header to file.\n");
  1348.             exit(20);
  1349.             }
  1350.         /* Translate */
  1351.         pagelines=0;
  1352.         while(getbit()); /* flush Panafax 1 bits */
  1353.         while(!getbit()); /* flush initial EOL */
  1354.         if(debug)
  1355.             printf(" = EOL\n");
  1356.         dopage=1;
  1357.         if(!setjmp(where)) /* returns -1 magically on DLE ETX */
  1358.             while(1)
  1359.                 {
  1360.                 /* loop over lines */
  1361.                 linenbits=npelschk=0;
  1362.                 while(1)
  1363.                     {
  1364.                     /* loop over symbols */
  1365.                     symbol=getwhite();
  1366.                     if(debug)
  1367.                         {
  1368.                         printf(" = %d White", symbol&0x0fff);
  1369.                         if(symbol&MAKEUP)
  1370.                             printf(" Makeup\n");
  1371.                         else
  1372.                             printf("\n");
  1373.                         }
  1374.                     bitcount=symbol&0x0fff;
  1375.                     if(symbol&MAKEUP)
  1376.                         {
  1377.                         if(symbol==SEVENOHS)
  1378.                             break;
  1379.                         symbol=getwhite();
  1380.                         if(debug)
  1381.                             {
  1382.                             printf(" = %d White", symbol&0x0fff);
  1383.                             if(symbol&MAKEUP)
  1384.                                 printf(" Makeup\n");
  1385.                             else
  1386.                                 printf("\n");
  1387.                             }
  1388.                         if(symbol&MAKEUP)
  1389.                             {
  1390.                             printf("Error: sequential makeups.\n");
  1391.                             abortpage();
  1392.                             dopage=0;
  1393.                             break;
  1394.                             }
  1395.                         bitcount+=symbol&0x0fff;
  1396.                         }
  1397.                     npelschk+=bitcount;
  1398.                     putwhite(bitcount);
  1399.                     symbol=getblack();
  1400.                     if(debug)
  1401.                         {
  1402.                         printf(" = %d Black", symbol&0x0fff);
  1403.                         if(symbol&MAKEUP)
  1404.                             printf(" Makeup\n");
  1405.                         else
  1406.                             printf("\n");
  1407.                         }
  1408.                     bitcount=symbol&0x0fff;
  1409.                     if(symbol&MAKEUP)
  1410.                         {
  1411.                         if(symbol==SEVENOHS)
  1412.                             break;
  1413.                         symbol=getblack();
  1414.                         if(debug)
  1415.                             {
  1416.                             printf(" = %d Black", symbol&0x0fff);
  1417.                             if(symbol&MAKEUP)
  1418.                                 printf(" Makeup\n");
  1419.                             else
  1420.                                 printf("\n");
  1421.                             }
  1422.                         if(symbol&MAKEUP)
  1423.                             {
  1424.                             printf("Error: sequential makeups.\n");
  1425.                             abortpage();
  1426.                             dopage=0;
  1427.                             break;
  1428.                             }
  1429.                         bitcount+=symbol&0x0fff;
  1430.                         }
  1431.                     npelschk+=bitcount;
  1432.                     putblack(bitcount);
  1433.                     }
  1434.                 if(!dopage)
  1435.                     break;
  1436.                 if(npelschk&&(npelschk!=1728))
  1437.                     printf("Line %d bad; %d pels.\n", pagelines/(2-dcsvr), npelschk);
  1438.                 pcxwriteline(2-dcsvr);
  1439.                 while(!getbit());
  1440.                 if(debug)
  1441.                     printf(" = EOL\n");
  1442.                 }
  1443.         else
  1444.             pcxwriteline(2-dcsvr);
  1445.         pcxwriteheader();
  1446.         close(pcxfd);
  1447.         printf("Page translate completed.\n");
  1448.         }
  1449.     }
  1450.